﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using EventAggregationQuickStart;
using System.Windows.Forms;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;

namespace ModuleA
{
    public class ModuleAPresenter : IModule
    {
        IModuleAView view;
        IRegionManagerService regionManager;
        private IEventAggregator eventAggregator;

        public ModuleAPresenter(IModuleAView view, IRegionManagerService regionManager, IEventAggregator eventAggregator)
        {
            this.view = view;
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;

            // Subscribe to the SomeServiceEvent
            SomeServiceEvent customEvent = eventAggregator.GetEvent<SomeServiceEvent>();
            customEvent.Subscribe(SomeEventEventHandler, false);

        }

        // Event Handler for the event published somewhere else in the system
        private void SomeEventEventHandler(DataForSomeServiceEvent data)
        {
            view.ShowDataFromEvent(data.SomeData);
        }

        #region IModule Members

        public void ShowView()
        {
            // Display the view in a region named 'Main'
            regionManager.ShowView("Main", view);
        }

        #endregion

        internal void Button1ActionRequested()
        {
            // Publish the Button1Event
            Button1Event customEvent = eventAggregator.GetEvent<Button1Event>();
            customEvent.Publish(new EventArgs());
        }
    }
}
